package role;

import role.Creature;

import java.io.*;
import java.util.Objects;

import static rpg.oop.RPG.randomGenerator;

public class Player extends Creature  {
    private static final long serialVersionUID = 1L;
    public int xp;
    public int hpPotions;
    public int money;

    public Player() {

    }

    public Player(String name, String role) {
        super(name, role, 1);
    }

    /**
     * 获得经验值
     * @param enemy 被玩家杀死的敌人
     * @return      增长的经验值
     */
    public int gainXp(Creature enemy) {
        int baseXp = 20;
        int randomXp = randomGenerator.nextInt(0, 40);
        int gainedXp = enemy.level * (baseXp + randomXp);
        xp += gainedXp;
        return gainedXp;
    }

    /**
     * 检查玩家是否该升级
     */
    public boolean checkLevelUp() {
        if (xp >= level * 40) {
            while (xp >= level * 40){
                xp = xp - (level * 40);
                level++;
                levelUp();
            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 计算玩家当前拥有的经验值和升级所需要的经验值
     * @return 包含了经验值信息的字符串
     */
    public String getXp() {
        return xp + "/" + (level * 40);
    }

    /**
     * 对玩家的各种属性进行初始化
     */
    public void initialize() {
        power = 0;
        intelligence = 0;
        agile = 0;
        defense = 0;
        int baseHp = 0;
        int baseAttack = 0;
        xp = 0;
        hpPotions = 3;
        level = 1;
        money = 200;
        switch (role) {
            case "战士":
                power = 7;
                intelligence = 2;
                agile = 3;
                break;
            case "游侠":
                power =4;
                intelligence = 4;
                agile = 4;
                break;
            case "术士":
                power = 2;
                intelligence = 7;
                agile = 3;
                break;
        }
        curAttribute(power, intelligence, agile);
        curHp = maxHp;
    }


    //当前属性
    public void curAttribute(int power, int intelligence, int agile){
        if (Objects.equals(role, "战士")){
            maxHp = ZSHp(power, intelligence, agile);
            attack = ZSAttack(power, intelligence, agile);
            defense = ZSDfense(power, intelligence, agile);
        }else if (Objects.equals(role, "游侠")){
            maxHp = YXHp(power, intelligence, agile);
            attack = YXAttack(power, intelligence, agile);
            defense = YXDfense(power, intelligence, agile);
        }else if (Objects.equals(role, "术士")){
            maxHp = FSHp(power, intelligence, agile);
            attack = FSAttack(power, intelligence, agile);
            defense = FSDfense(power, intelligence, agile);
        }
    }

    //法师防御
    private int FSDfense(int power, int intelligence, int agile) {
        double b =power * 0.5 + agile * 2 + agile *0.5;
        return (int) b;
    }

    //法师攻击
    private int FSAttack(int power, int intelligence, int agile) {
        return intelligence *5;
    }

    //法师血量
    private int FSHp(int power, int intelligence, int agile) {
        return power + intelligence * 3 + agile;
    }

    //游侠防御函数
    private int YXDfense(int power, int intelligence, int agile) {
        double b =power * 1.2 + agile * 2.5;
        return (int) b;
    }

    //游侠攻击函数
    private int YXAttack(int power, int intelligence, int agile) {
        double b =power*1.1 + intelligence + agile * 1.9;
        return (int) b;
    }

    //游侠血量方法
    private int YXHp(int power, int intelligence, int agile) {
        double b =power + agile * 3 +intelligence*0.5;
        return (int) b;
    }

    //战士防御函数
    private int ZSDfense(int power, int intelligence, int agile) {
        double b =power * 1.2 + agile * 0.5;
        return (int) b;
    }

    //战士攻击函数
    private int ZSAttack(int power, int intelligence, int agile) {
        return (power * 2 + agile) ;
    }

    //战士血量函数
    private int ZSHp(int power, int intelligence, int agile) {
        return power * 5 + agile * 2;
    }


    /**
     * 升级
     */
    public void levelUp() {
        int lvPower = 0;
        int lvIntelligence = 0;
        int lvAgile = 0;
        switch (role) {
            case "战士":
                lvPower = 2;
                lvIntelligence = 1;
                lvAgile = 2;
                break;
            case "游侠":
                lvPower = 1;
                lvIntelligence = 1;
                lvAgile = 3;
                break;
            case "术士":
                lvPower = 1;
                lvIntelligence = 3;
                lvAgile = 1;
                break;

        }
        power += lvPower;
        intelligence += lvIntelligence;
        agile += lvAgile;
        curAttribute(power, intelligence, agile);
        curHp = maxHp;

    }



}
